import { Color } from "cc";
import { EAnimationPlayMode, EBuff, ESkill, ESkillType } from "../Config/Enum";

// 定义武学接口
export interface ISkill {
    id: number;
    name: string;
    level: number;
    cd: number;
    type: ESkill;
    skillType: ESkillType;
    moveSpeed: number;
    playSpeed: number; // 新增播放速度属性
    duration: number;
    ap: number;
    buffAp: number; // 默认buffAp为0
    effectChance: number;
    effectDuration?: number; // 新增效果持续时间属性
    slashColor: Color;
    animationPlayMode: EAnimationPlayMode;
    criticalChance: number;
    buffType: EBuff;
    scale: number[];
    numOfBullets: number;
    damage: number; // 默认伤害为0
    runeId: number; // 默认符文ID为0
    penetratePercent: number; // 穿透百分比
    penetrateCount: number; // 穿透数量，可选属性
}

// 实现武学类
export class Skill implements ISkill {
    id: number;
    name: string;
    level: number;
    maxLevel: number = 10; // 假设最大等级为10
    cd: number;
    type: ESkill;
    skillType: ESkillType;
    moveSpeed: number;
    playSpeed: number = 1; // 默认播放速度为1
    duration: number = 0;
    ap: number = 0;
    buffAp: number = 0; // 默认buffAp为0
    penetratePercent: number = 0; // 穿透百分比
    penetrateCount: number = 0; // 穿透数量，可选属性
    effectChance: number = 0.1;
    effectDuration: number = 0; // 默认效果持续时间为0
    slashColor: Color = Color.YELLOW;
    animationPlayMode: EAnimationPlayMode;
    criticalChance: number;
    buffType: EBuff;
    scale: number[] = [1,1];
    numOfBullets: number = 1;
    damage: number = 0; // 默认伤害为0
    runeId: number = 0; // 默认符文ID为0
    
    constructor(
        id: number, 
        name: string, 
        level: number, 
        maxLevel: number = 10, // 最大等级默认为10
        cd: number, 
        type: ESkill,
        skillType: ESkillType, 
        moveSpeed: number,
        playSpeed: number = 1, // 默认播放速度为1
        duration: number,
        ap: number,
        buffAp: number = 0, // 默认buffAp为0
        effectChance: number,
        effectDuration: number = 0, // 默认效果持续时间为0
        slashColor: Color,
        animationPlayMode: EAnimationPlayMode,
        criticalChance: number,
        buffType: EBuff,
        scale: number[] = [1,1],
        numOfBullets: number = 1,
        damage: number = 0,
        runeId: number = 0, // 默认符文ID为0
        penetratePercent: number = 0, // 默认穿透为0
        penetrateCount: number = 0
    ) {  
        this.id = id;
        this.name = name;
        this.level = level;
        this.maxLevel = maxLevel; // 设置最大等级
        this.cd = cd;
        this.type = type;
        this.skillType = skillType;
        this.moveSpeed = moveSpeed;
        this.playSpeed = playSpeed; // 设置播放速度
        this.duration = duration;
        this.ap = ap;
        this.buffAp = buffAp;
        this.effectChance = effectChance;
        this.effectDuration = effectDuration; // 设置效果持续时间
        this.slashColor = slashColor;
        this.animationPlayMode = animationPlayMode;
        this.criticalChance = criticalChance;
        this.buffType = buffType;
        this.scale = scale;
        this.numOfBullets = numOfBullets;
        this.damage = damage; // 初始化伤害
        this.runeId = runeId; // 初始化符文ID
        this.penetratePercent = penetratePercent; // 初始化穿透
        this.penetrateCount = penetrateCount; // 初始化穿透数量
    }

    // 序列化武学数据
    getPackage(): any {
        return {
            id: this.id,
            name: this.name,
            level: this.level,
            maxLevel: this.maxLevel,
            cd: this.cd,
            type: this.type,
            skillType: this.skillType,
            moveSpeed: this.moveSpeed,
            playSpeed: this.playSpeed, // 添加播放速度到序列化数据
            duration: this.duration,
            ap: this.ap,
            buffAp: this.buffAp,
            effectChance: this.effectChance,
            effectDuration: this.effectDuration, // 添加效果持续时间到序列化数据
            slashColor: this.slashColor.toHEX('#rrggbb'),
            animationPlayMode: this.animationPlayMode,
            criticalChance: this.criticalChance,
            buffType: this.buffType,
            scale: this.scale,
            numOfBullets: this.numOfBullets,
            damage: this.damage,
            runeId: this.runeId, // 添加符文ID到序列化数据
            penetratePercent: this.penetratePercent, // 添加穿透到序列化数据
            penetrateCount: this.penetrateCount // 添加穿透数量到序列化数据
            
        };
    }

    // 从序列化数据恢复武学
    setPackage(data: any): void {
        this.id = data.id;
        this.name = data.name;
        this.level = data.level;
        this.maxLevel = data.maxLevel || 10; // 如果没有提供最大等级，则默认为10
        this.cd = data.cd;
        this.type = data.type;
        this.skillType = data.skillType;
        this.moveSpeed = data.moveSpeed;
        this.playSpeed = data.playSpeed || 1; // 如果没有提供播放速度，则默认为1
        this.duration = data.duration;
        this.ap = data.ap;
        this.buffAp = data.buffAp || 0; // 如果没有提供buffAp，则默认为0
        this.effectChance = data.effectChance;
        this.effectDuration = data.effectDuration || 0; // 如果没有提供效果持续时间，则默认为0
        this.damage = data.damage || 0; // 恢复伤害，默认为0
        this.runeId = data.runeId || 0; // 恢复符文ID，默认为0
        this.penetratePercent = data.penetratePercent || 0; // 恢复穿透，默认为0
        this.penetrateCount = data.penetrateCount || 0; // 恢复穿透数量，默认为0
        
        // 恢复颜色
        if (data.slashColor) {
            this.slashColor = Color.fromHEX(new Color(), data.slashColor);
        }
        
        this.animationPlayMode = data.animationPlayMode;
        this.criticalChance = data.criticalChance;
        this.buffType = data.buffType;
        this.scale = data.scale;
        this.numOfBullets = data.numOfBullets;
    }


//     clone(): Skill {
//     const cloned = new Skill(
//         this.id,
//         this.name,
//         this.level,
//         this.maxLevel,
//         this.cd,
//         this.type,
//         this.skillType,
//         this.moveSpeed,
//         this.playSpeed,
//         this.duration,
//         this.ap,
//         this.buffAp,
//         this.effectChance,
//         this.effectDuration,
//         this.slashColor,
//         this.animationPlayMode,
//         this.criticalChance,
//         this.buffType,
//         [...this.scale], // 复制数组
//         this.numOfBullets,
//         this.runeId,
//         this.damage
//     );
//     return cloned;
// }
clone(): Skill {
    const cloned = new Skill(
        this.id,
        this.name,
        this.level,
        this.maxLevel,
        this.cd,
        this.type,
        this.skillType,
        this.moveSpeed,
        this.playSpeed,
        this.duration,
        this.ap,
        this.buffAp,
        this.effectChance,
        this.effectDuration,
        this.slashColor,
        this.animationPlayMode,
        this.criticalChance,
        this.buffType,
        [...this.scale],
        this.numOfBullets,
        this.damage,
        this.runeId,
        this.penetratePercent
    );
    return cloned;
}
}